package com.huizhonglm.diap.ui.user;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.DistrictUtil;
import com.huizhonglm.diap.model.District;
import com.huizhonglm.diap.api.model.user.AddrInfo;

import java.util.List;

/**
 * 推荐关注用户列表适配器
 *
 * @author LiXiaolong
 * @version 1.0
 * @since 2014年8月11日下午4:25:25
 */
public class AddrListAdapter extends BaseAdapter {
    private Context mContext;
    private List<AddrInfo> mData;

    public AddrListAdapter(Context con, List<AddrInfo> data) {
        this.mContext = con;
        this.mData = data;
    }

    public void setData(List<AddrInfo> data) {
        this.mData = data;
    }

    @Override
    public int getCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public AddrInfo getItem(int position) {
        if (mData != null && mData.size() > 0) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(mContext, R.layout.item_addr_list, null);
            holder.item = view.findViewById(R.id.addr_list_item);
            holder.topView = view.findViewById(R.id.padding_top_view);
            holder.defImg = view.findViewById(R.id.def_addr_img);
            holder.name = (TextView) view.findViewById(R.id.reciver_name_txt);
            holder.phone = (TextView) view.findViewById(R.id.phone_number_txt);
            holder.addr = (TextView) view.findViewById(R.id.detail_addr_txt);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (position == 0) {
            holder.topView.setVisibility(View.VISIBLE);
        } else {
            holder.topView.setVisibility(View.GONE);
        }

        final AddrInfo info = getItem(position);
        if (info != null) {
            String label;
            if ("Y".equals(info.getIsDefault())) {
                label = "【默认】";
                holder.item.setBackgroundResource(R.drawable.bg_def_addr);
                holder.defImg.setVisibility(View.VISIBLE);
            } else {
                label = "";
                holder.item.setBackgroundResource(R.drawable.bg_white_line_bottom);
                holder.defImg.setVisibility(View.INVISIBLE);
            }
            holder.name.setText(info.getReceiver());
            holder.phone.setText(info.getPhoneNum());
            if (!EmptyUtil.isEmpty(info.getDistrictCode())) {
                String addr;
                District district = DistrictUtil.getInstance().getDistrict(info.getDistrictCode());
                String pn = district.getProvName();
                String cn = district.getCityName();
                String dn = district.getDistName();
                switch (pn) {
                    case "北京":
                    case "上海":
                    case "天津":
                    case "重庆":
                        addr = cn + dn + info.getDetailAddr();
                        break;
                    case "台湾":
                    case "香港":
                    case "澳门":
                        addr = pn + cn + dn + info.getDetailAddr();
                        break;
                    default:
                        addr = pn + "省" + cn + dn + info.getDetailAddr();
                        break;
                }
                holder.addr.setText(label + addr);
            } else {
                holder.addr.setText("");
            }
        }
        return view;
    }

    private class ViewHolder {
        private View topView;
        private TextView name;
        private TextView phone;
        private TextView addr;
        private View defImg;
        private View item;
    }

}
